<?php
/**
 * @Author: lianlianzan(13040@qq.com)
 * @Date:   2018-05-14 16:40:00
 * @Last Modified by:   lianlianzan
 * @Last Modified time: 2021-02-27 12:08:48
 */

namespace app\weixin_manage\controller;
use app\common\controller\Backend;

class Event extends Backend {

	function initialize() {
		parent::initialize();
		$this->saveAndGetConfigIdentify('', '事件管理', '公众号管理', 'event');

		$this->weixinEventDao = model('weixin_manage/WeixinEvent');
	}

	function index() {
		if (!$this->wid) {
			$this->adminError('微信wid有误');
		}
		$map = array(
			'wid'   => $this->wid,
		);
		$count = $this->weixinEventDao->countSearch($map);
		$Page = new \org\util\PageBootstrap($count, config('app.per_page'), $parameter);
		$pageShow = $Page->show();
		list($list) = $this->weixinEventDao->search($map, $Page->getLimit());
		$this->assign('count', $count);
		$this->assign('pageShow', $pageShow);
		$this->assign('list', $list);

		$this->assign('msgtype_config', config('msgtype_config')); //消息类型
		
		return $this->fetch();
	}

	public function listorders() {
		$result = parent::listorders($this->weixinEventDao);

		if ($result !== false) {
			$this->success('排序更新成功');
		} else {
			$this->error('排序更新失败');
		}
	}

	public function add() {
		$id = input('param.id', '', 'intval');

		if ($id) {
			$info = $this->weixinEventDao->getInfo($id);
		} else {
			$info = [
				'status' => 1
			];
		}
		$this->assign('id', $id);
		$this->assign('info', $info);

		return $this->fetch();
	}

	public function doAdd() {
		$id = input('post.id', '', 'intval');
		$data = input('post.data/a', '', 'pwEscape');

		if (strpos($data['event_text'], '{d}') !== false) {
			$data['is_regular'] = 1;
		}

		//$data['url'] = $_POST['data']['url']; //不过滤
		$data['link'] = ueditorContent($data['link']);

		if ($data['event_msgtype'] == 2) { //图片
			//普通上传图片
			$uploadInfo = $this->comuploadFile('kszx/event', 'picurl', $info['picurl'], array(), 0);
			$uploadInfo && $data['picurl'] = $uploadInfo['saveFullPath'];
			//print_r($uploadInfo);exit;

			//将图片上传到微信后台
			if ($uploadInfo) {
				set_time_limit(0);
				$filename = $uploadInfo['savepath'].$uploadInfo['savename'];
				//print_r($filename);exit;
				//db('Aaaa')->insert(['space'=>'weixin_manage', 'module'=>'event', 'flag'=>'filename', 'content'=>$filename]);

				//获取微信公众号信息
				$weixinAccountInfo = $this->weixinAccountDao->getInfo($this->wid);

				$options = array(
					'token'=>$weixinAccountInfo['token'], //填写你设定的key
					'encodingaeskey'=>$weixinAccountInfo['aeskey'], //填写加密用的EncodingAESKey
					'appid'=>$weixinAccountInfo['appid'], //填写高级调用功能的app id, 请在微信开发模式后台查询
					'appsecret'=>$weixinAccountInfo['appsecret'], //填写高级调用功能的密钥
					'partnerid'=>'', //财付通商户身份标识，支付权限专用，没有可不填
					'partnerkey'=>'', //财付通商户权限密钥Key，支付权限专用
					'paysignkey'=>'' //商户签名密钥Key，支付权限专用
				);
				$weObj = new \wechat\Wechat($options);

				//$dataWeixin = array(
				//	'media' => $_FILES['picurl'],
				//);
				$dataWeixin = array();
				db('Aaaa')->insert(['space'=>'weixin_manage', 'module'=>'event', 'flag'=>'FILES', 'content'=>pw_var_export($_FILES['picurl'])]);
				$result = $weObj->uploadMedia($dataWeixin, 'image', $filename);
				db('Aaaa')->insert(['space'=>'weixin_manage', 'module'=>'event', 'flag'=>'result', 'content'=>pw_var_export($result)]);
				//{"type":"image","media_id":"bohSnA_sUfyNNkehT96NNAsYRJJpv4ZDBGvFn7hkuJLEP3JL8CzsS-S20q9yL0jO","created_at":1531215297}
				if ($result !== false) {
					$data['media_id'] = $result['media_id'];
					//删除原图
					unlink($filename);
				} else {
					db('Aaaa')->insert(['space'=>'weixin_manage', 'module'=>'event', 'flag'=>'error', 'content'=>'菜单生成失败，错误代码：'.$weObj->errCode.'；错误提示：'.$weObj->errMsg]);
					$this->error('菜单生成失败，错误代码：'.$weObj->errCode.'；错误提示：'.$weObj->errMsg);
				}
			}
		} else {
			//普通上传图片
			$uploadInfo = $this->comuploadFile('kszx/event', 'picurl', $info['picurl']);
			$uploadInfo && $data['picurl'] = $uploadInfo['saveFullPath'];
		}

		if ($id) {
			$result = $this->weixinEventDao->baseUpdateData($id, $data);
			if ($result !== false) {
				$this->success('保存成功', url('weixin_manage/event/index', ['wid'=>$this->wid]));
			} else {
				$this->error('保存失败'.showDbError($this->weixinEventDao));
			}
		} else {
			$data['wid'] = $this->wid;
			$data['create_time'] = $this->ts;
			$result = $this->weixinEventDao->baseAddData($data);
			if ($result !== false) {
				$this->success('添加成功', url('weixin_manage/event/index', ['wid'=>$this->wid]));
			} else {
				$this->error('添加失败'.showDbError($this->weixinEventDao));
			}
		}
	}

	public function doDelete() {
		$id = input('param.id', '', 'intval');
		if (!$id) {
			$this->error('非法ID');
		}

		$result = $this->weixinEventDao->del($id);
		if ($result) {
			$this->success('删除成功');
		} else {
			$this->error('删除失败'.showDbError($this->weixinEventDao));
		}
	}

	//更新至微信
	// public function updateCache() {
	// 	$options = array(
	// 		'token'=>'e0a71dd51c89cdb3048c738953a79c1e', //填写你设定的key
	// 		'encodingaeskey'=>'jlZw73CkpskeIRLrboqqGNQlf8SUINYxboz5xjotP9J', //填写加密用的EncodingAESKey
	// 		'appid'=>'wx242a6b1ac3048333', //填写高级调用功能的app id, 请在微信开发模式后台查询
	// 		'appsecret'=>'0f528dc44306d2a4b5b33287962567a6', //填写高级调用功能的密钥
	// 		'partnerid'=>'', //财付通商户身份标识，支付权限专用，没有可不填
	// 		'partnerkey'=>'', //财付通商户权限密钥Key，支付权限专用
	// 		'paysignkey'=>'' //商户签名密钥Key，支付权限专用
	// 	);
	// 	$weObj = new \Common\Lib\Util\Wechat($options);
	// 	$weObj->valid();

	// 	$newmenu = $this->Dao->getMenu();
	// 	$result = $weObj->createMenu($newmenu);

	// 	$fields = array(
	// 		'flag'  => 'admin_create',
	// 		'test'  => $result,
	// 	);
	// 	//M('Test')->add($fields);
	// 	$this->success("排序更新成功！");
	// }

}